<?php

use \GatewayWorker\Lib\Gateway;

//加载控制器
$file=scandir(__DIR__.'/controller');
array_splice($file,0,2);
foreach($file as $value){require_once __DIR__.'/controller/'.$value;}

//加载方法
$file=scandir(__DIR__.'/method');
array_splice($file,0,2);
foreach($file as $value){require_once __DIR__.'/method/'.$value;}

class LH{
  static $hallUser=[];//name room
  static $roomList=[];//name now num user

  static $gameUser=[];//name room
  static $gameList=[];//num user

  static function onConnect($cid){

  }

  static function onMessage($cid, $msg){
    //心跳 返回信息(总人数)
    if($msg['r']=='/main/ping')lhcontrol('Main','ping',$cid,$msg);

    //大厅
      //进入
      if($msg['r']=='/hall/enter')lhcontrol('Hall','enter',$cid,$msg,['name']);
      //聊天
      if($msg['r']=='/hall/chat')lhcontrol('Hall','chat',$cid,$msg,['content']);
      //建房
      if($msg['r']=='/hall/room/set')lhcontrol('Hall','setRoom',$cid,$msg,['name']);

    //房间
      //加入
      if($msg['r']=='/room/enter')lhcontrol('Room','enter',$cid,$msg,['id']);
      //离开
      if($msg['r']=='/room/leave')lhcontrol('Room','leave',$cid,$msg);
      //聊天
      if($msg['r']=='/room/chat')lhcontrol('Room','chat',$cid,$msg,['content']);
      //修改房间名
      //修改英雄
      if($msg['r']=='/room/hero')lhcontrol('Room','hero',$cid,$msg,['hero']);
      //准备游戏
      if($msg['r']=='/room/ready')lhcontrol('Room','ready',$cid,$msg);
      //取消准备
      if($msg['r']=='/room/unready')lhcontrol('Room','unReady',$cid,$msg);

    //游戏
      //加入
      if($msg['r']=='/game/enter')lhcontrol('Game','enter',$cid,$msg,['name','key','hero','now','num','owner']);
      //离开
      if($msg['r']=='/game/leave')lhcontrol('Game','leave',$cid,$msg);
      //开始
      if($msg['r']=='/game/start')lhcontrol('Game','start',$cid,$msg,['fleet']);
      //聊天
      if($msg['r']=='/game/chat')lhcontrol('Game','chat',$cid,$msg,['content']);
      //行为请求
      if($msg['r']=='/game/act/req')lhcontrol('Game','actReq',$cid,$msg,['type','param']);
      //行为运作
      if($msg['r']=='/game/act')lhcontrol('Game','act',$cid,$msg,['id','type','param']);
      //错误提示
      if($msg['r']=='/game/error')lhcontrol('Game','error',$cid,$msg,['id','content']);
  }

  static function onClose($cid){
    //如果离开者在房间中
    if(isset(self::$hallUser[$cid]['room'])){
      lhcontrol('Room','leave',$cid,null);
    }
    //如果离开者在游戏中
    if(isset(self::$gameUser[$cid])){
      lhcontrol('Game','leave',$cid,null);
    }
    //如果离开者在大厅组中
    if(isset(self::$hallUser[$cid])){
      unset(self::$hallUser[$cid]);
      $result=json_encode(['r'=>'/hall/leave','data'=>$cid]);
      Gateway::sendToGroup('hall',$result);
    }
  }
}

//定义路由方法||类名 方法名 cid 数据 必传参数
function lhcontrol($cont,$func,$cid,$msg,$require=null){
  //参数处理
  $allparams=[];
  if($require){
    foreach ($require as $value){
      if(!isset($msg[$value])){
        $result=json_encode(['r'=>'/main/error','code' => 40,'msg' => '参数错误']);
        Gateway::sendToClient($cid, $result);
        return;
      }
      array_push($allparams,$msg[$value]);
    }
  }

  //调用控制器
  $cont1='lh\\controller\\'.$cont;
  $cont1::$func($cid,...$allparams);
}